package com.neuedu.thread;

public class TicketPoll {


    //票默认有 50
    private int num = 50;


//    public synchronized   int getTicket(){
//        return num--;
//    }

    public    int getTicket() {
        synchronized (TicketPoll.class){
//            try {
//                Thread.sleep(9999999);
//            } catch (InterruptedException e) {
//                throw new RuntimeException(e);
//            }
            return num--;
        }

    }


    public static void main(String[] args) {
        TicketPoll ticketPoll = new TicketPoll();


        new Thread(){
            @Override
            public void run() {
               while (true){
                   int ticketNum = ticketPoll.getTicket();
                   if(ticketNum <= 0){
                       break;
                   }
                   System.out.println("王小华正在--》"+ticketNum);
                   try {
                       Thread.sleep(1000);
                   } catch (InterruptedException e) {
                       throw new RuntimeException(e);
                   }
               }
            }
        }.start();

        new Thread(){
            @Override
            public void run() {
                while (true){
                    int ticketNum = ticketPoll.getTicket();
                    if(ticketNum <= 0){
                        break;
                    }
                    System.out.println("牛晓磊正在--》"+ticketNum);
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        }.start();
    }


}
